package com.rom.linkedlist;

/**
 * 双向链表的实现
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        DoubleLinkedList list = new DoubleLinkedList();
        list.add(new BHeroNode(1,"zhangsan","张三"));
        list.add(new BHeroNode(3,"lisi","李四"));
        list.add(new BHeroNode(5,"wanger","王二"));
        list.add(new BHeroNode(7,"mazi","麻子"));
        System.out.println("链表的遍历-------------");
        list.list();
        System.out.println("链表的删除-------------");
//        list.delete(1);
        list.delete(7);
        list.list();
        System.out.println("链表的插入-------------");
        list.insert(new BHeroNode(4,"666","张三666"));
        list.list();
        System.out.println("链表的更新-------------");
        list.update(new BHeroNode(7,"xiongmazi","熊麻子"));
        list.list();
    }
}

//定义链表管理BHeroNode
class DoubleLinkedList {

    //先初始化一个头节点
    private BHeroNode head = new BHeroNode(0,"","");

    //添加节点,将节点加入链表末尾.
    public  void add(BHeroNode node){

        BHeroNode temp = head;

        while (true){
            if(temp.next == null){
                temp.next = node;
                node.pre = temp;
                break;
            }
           temp = temp.next;
        }

    }
    //节点的插入
    public void insert(BHeroNode node){

        BHeroNode temp = head;

        boolean flag = false;

        while (true){

            if(temp.next == null){
                //加入末尾
//                temp.next = node;
                //头结点
                add(node);
                break;
            }
            if(temp.next.no > node.no){
                //表示将系节点插入当前节点之前
                flag = true;

//                node.next = temp.next;
//                temp.next = node;
                break;
            }else if(temp.next.no == node.no ){
//                System.out.println("节点已存在");
                break;
            }
            temp = temp.next;
        }
        if(flag){
//            插入当前节点的后面
//            node要先和temp.next建立连接
            node.next = temp.next;
            temp.next.pre = node;
            //nodo和temp建立连接
            temp.next = node;
            node.pre = temp;
        }else{
            System.out.println("插入失败");
        }
    }

    //节点的遍历。
    public void list(){

        BHeroNode temp = head;
        while (true){
            if(temp.next == null){
                break;
            }
            if(temp.next != null){
                System.out.println(temp.next);

            }
            temp = temp.next;
        }
    }
    //节点的修改
    public void update(BHeroNode node){

        //辅助节点从第一个开始
        BHeroNode temp = head.next;

        while (true){
            if(temp == null){
//                结束
                break;
            }

            if(temp.no == node.no){
                //节点找到
                temp.name = node.name;
                temp.nikename = node.nikename;
                //修改完结束
                break;
            }
            temp = temp.next;
        }
    }

    //链表的删除
    public void delete(int no){

        BHeroNode temp = head.next;
        if(temp == null){

            System.out.println("链表已经为空");
        }
        while (true){

            if(temp.no == no){
                //找到了删除的节点
                temp.pre.next = temp.next;
                if(temp.next!=null){
                    temp.next.pre = temp.pre;
                }
                break;
            }
            temp = temp.next;
        }
    }
}

class BHeroNode{

    public int no;
    public String name;
    public String nikename;
    public BHeroNode pre;
    public BHeroNode next;

    public BHeroNode(int no, String name, String nikename) {
        this.no = no;
        this.name = name;
        this.nikename = nikename;
    }

    @Override
    public String toString() {
        return "BHeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nikename='" + nikename + '\'' +
                '}';
    }
}